import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../AppConfig/Config.dart';
import '../../AppConfig/RouteAct.dart';
import '../../AppConfig/ShowDialog.dart';
import '../../http/NoLoadingGetApi.dart';
import '../../l10n/messages.dart';
import 'FeedbackDetails.dart';
import 'FeedbackProgress.dart';
import 'package:platform/platform.dart';
class FeedBack extends StatefulWidget {
  const FeedBack({super.key});

  @override
  State<FeedBack> createState() => _FeedBackState();
}

class _FeedBackState extends State<FeedBack> {
  List dataList=[];
  List titleList=["ETC、金融使用投诉","订金、运费等交易问题","产品体验反馈"];
  List labelList=["联系客服","联系客服","功能异常、产品建议等"];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getPageAction();
  }

  void getPageAction(){
    NoLoadinggetApiManager.getData(
      context,
      "api/system/dev/dict/list",
      {'category':'BIZ','parentId':'1805865486975766530'},
          (responseData)  {
        setState(() {
          dataList=responseData['data'];
        });

      },
    );
  }
  _makePhoneCall(String phoneNumber) async {
    if (LocalPlatform().isAndroid) {
      var status = await Permission.phone.status;
      if (status.isGranted) {
        // 权限已被授予
        // callPhone();
        final AndroidIntent intent = AndroidIntent(
          action: 'android.intent.action.CALL',
          data: Uri.encodeFull('tel:$phoneNumber'),
        );
        await intent.launch();
      } else if (status.isDenied) {
        showCallPhoneFirstDialog(context);
        // 权限被拒绝，但用户可以再次授权
        status = await Permission.phone.request();
        if (status.isGranted) {
          // 用户同意了权限
          // callPhone();
          final AndroidIntent intent = AndroidIntent(
            action: 'android.intent.action.CALL',
            data: Uri.encodeFull('tel:$phoneNumber'),
          );
          await intent.launch();
        } else {
          showCallPhoneDialog(context);
          // 用户拒绝权限，无法发起电话
          // 可以在此处展示信息提示用户需要权限
        }
      } else if (status.isPermanentlyDenied) {
        // 权限被永久拒绝，需要用户手动开启权限
        showCallPhoneDialog(context);
      }

    }else{
      final url =Uri.parse('tel:$phoneNumber');
      if (await canLaunchUrl(url)) {
        await launchUrl(url);
      } else {
        throw '无法拨打电话：$url';
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context)!;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        backgroundColor: Colors.white,
        centerTitle: true,
        title: Text(messages['意见反馈'],style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
        actions: [
          TextButton(onPressed: (){
            Navigator.push( context, FadeRoute(page:FeedbackProgress()));
          },
              child: Text("意见反馈记录",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 14.sp),))
        ],
      ),
      body: ListView(
        children: [
          Container(
            // height: 173.h,
            margin: EdgeInsets.only(left:10.w,right: 10.w,top: 8.h ),
            color: Colors.white,
            child:Column(
              children: [
                SizedBox(height: 10.h,),
                Row(
                  children: [
                    SizedBox(width: 20.w,),
                    Text("*",style: TextStyle(color: MyColors.themeColor,fontSize: 16.sp),),
                    Text("请选择反馈问题的类型",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w600,fontSize: 16.sp),)
                  ],
                ),
                SizedBox(height: 10.h,),
                Container(
                  color: MyColors.backgreyColor,
                  height: 1.h,
                ),
                SizedBox(height: 10.h,),
                Container(
                  height: 130.h,
                  child:  ListView.separated(
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: dataList.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Column(
                          children: [
                            SizedBox(height: 10.h,),
                            GestureDetector(
                              child:     Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text("     "+dataList[index]['dictLabel'],style: TextStyle(color:Colors.black,fontSize: MyFontSizes.commonrText.sp),),
                                  Row(
                                    children: [
                                      Text(labelList[index],style: TextStyle(color: Colors.black54,fontSize: 14.sp),),
                                      Icon(Icons.arrow_forward_ios,color:MyColors.authTipColor,size: 14,)
                                    ],
                                  ),
                                ],
                              ),
                              onTap: (){
                                if(index==dataList.length-1) {
                                  Navigator.push(context,
                                      FadeRoute(page: FeedbackDetails(parentId: dataList[index]['id'],)));
                                }else{
                                  _makePhoneCall("0574-86761267");
                                }
                              },
                            ),

                            SizedBox(height: 10.h,),
                          ],
                        );

                      },

                      separatorBuilder: (context, index) { //自定义分割线
                        return Container(
                          color: MyColors.backgreyColor,
                          height: 1.h,
                        );
                      }),
                ),

              ],
            ),
          )
        ],
      ),

    );
  }
}